//index.js

var websocket = require('../../utils/websocket.js');


//获取应用实例
var app = getApp()
    
var message = '';

var text = '';

var user = {};

var receives = [];

var pointid = '';

Page({
  data: {
    message : '',
    receives : {},
    user : {},
    rpshow:false
  },
  bindChange: function(e) {
        message = e.detail.value
  },
  //事件处理函数
  add: function(e) {
    console.log("add====xxxxxxxxxxs"+message)
   wx.navigateTo({
      url: '/pages/send/send',
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  sendMsg:function(e){
     var loginUser = app.getLoginUser();
      var msg = {uid:loginUser.uid,pwd:loginUser.pwd,msg:message};
      console.log("dianji=="+JSON.stringify(msg));
      websocket.send("msg",msg);
  },
  grab: function(e){
    console.log(JSON.stringify(e))
    pointid = e.currentTarget.dataset.id;
    console.log("pointid===="+pointid);
    this.setData({rpshow:true});
    console.log("open click....")
  },
  open:function(e){
   
    var pointId = pointid;
     var receiverId = app.getLoginUser().uid;
    wx.request({
      url: 'http://localhost:8888/msg/grab',
      data: {
        pointId:pointId,
        receiverId:receiverId
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        
          wx.navigateTo({
                url: '/pages/detail/detail?pointId='+pointId,
                success: function(res){
                   
                },
                fail: function() {
                  // fail
                },
                complete: function() {
                  // complete
                }
              })
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
   
    console.log("open..........")
  },
  onLoad: function () {
    
    var that = this
    console.log("onload....")

    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      user = userInfo;
      that.setData({user:user});
      var loginUser = app.getLoginUser();
      websocket.connect(loginUser, function(res) {
        receives.push({type:"r",data:JSON.parse(res.data)});
        console.log("收到消息："+res.data)
        that.setData({receives:receives});
       
      });
     
      
    })
  }
})
